/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * ArmItemFinalStoreVistor.java
 * business
 * 2012-11-20下午2:43:20
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.visitor;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.ArmItem;
import org.jackysoft.entity.DataItem;
import org.jackysoft.entity.Department;
import org.jackysoft.entity.ForwardStore;
import org.jackysoft.entity.ProductType;
import org.jackysoft.report.DataItemType;
import org.jackysoft.report.ItemUnit;
import org.jackysoft.report.ReportConstant;
import org.jackysoft.service.ForwardStoreService;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

/**
 * 期末结存访问者
 */
@Component(ReportConstant.DailyFinalStroeVistor)
public class ArmItemFinalStoreDailyVisitor extends AbstractArmItemVisitor {

	private static final Log logger = LogFactory
			.getLog(ArmItemFinalStoreDailyVisitor.class);
	private ForwardStoreService forwardService;	
	private DateTime createDate;
	private Department department;	
	private boolean starup = false;

	public void starup(Department department, DateTime cal) {

		this.department = department;
		this.createDate = cal==null?DateTime.now():cal;		
		starup = true;
		
	}

	@Override
	public void visite(ItemUnit item) {

		if (!starup) {
			logger.warn("请先调用 starup 操作");
			return;
		}
		if (item == null)
			return;
		ArmItem current = (ArmItem) item;
		Double monthValue = getAccumulation(current, DataItemType.ForwardStore);
		Double yearValue = getAtom(current,DataItemType.ForwardStore);
		if (monthValue == 0D || yearValue==0D || current.getItemType()!=ProductType.常规产品) {	

			ForwardStore fs = forwardService.get(department, createDate,current.getHost(),current.getItemType());
			DataItem tdi = DataItem.EmptyDataItem();
			if (fs != null) {
				tdi = fs.getDataItem().clone();				
			}
			dataItemUtil.setDataItem(current, DataItemType.ForwardStore,tdi);
			monthValue = tdi.getAccumulateValue();
			
		}

		// 月初值

		// 本日动工
		Double action = getAccumulation(current, DataItemType.Action);

		// 本日合格
		Double passed = getAccumulation(current, DataItemType.Passed);

		// 本日废品
		Double waster = getAccumulation(current, DataItemType.Waster);

		// 本日试销
		Double test = getAccumulation(current, DataItemType.Test);

		Double finalValue = monthValue + action - passed - waster - test;

		DataItem finalStore = getDataItem(current, DataItemType.FinalStore);
		if (finalStore == null) {

			finalStore = new DataItem();
			current.getDataItems().put(DataItemType.FinalStore.name(),
					finalStore);
		}

		finalStore.setAtomValue(finalValue);

	}

	@Resource
	public void setForwardService(ForwardStoreService forwardService) {
		this.forwardService = forwardService;
	}

}
